package dp;

/**
 * 712. 两个字符串的最小ASCII删除和
 * 创建时间：2024/6/28 下午4:06
 */
public class 两个字符串的最小ASCII删除和_712 {
    public int minimumDeleteSum(String s1, String s2) {
        int n = s1.length();
        int m = s2.length();
        int[][] dp = new int[n + 1][m + 1];
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            char c1 = s1.charAt(i - 1);
            sum += c1;
            for (int j = 1; j <= m; j++) {
                char c2 = s2.charAt(j - 1);
                if (c1 == c2) {
                    dp[i][j] = dp[i - 1][j - 1] + c1;
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }

        for (int i = 0; i < m; i++) {
            sum += s2.charAt(i);
        }
        return sum - 2 * (dp[n][m]);
    }
}
